﻿using System;
using System.Collections.Generic;
using BitBenderGames;
using UnityEngine;
using Yoozoo.Framework.Core;
using Yoozoo.Gta.Common;

namespace Gameplay.PVE.Survivor
{
    public class DeckTouchManager
    {
        private enum ETouchState
        {
            None,
            Down,
            Drag,
            Click,
        }

        private ETouchState touchState = ETouchState.None;
        private bool isActive;
        private int lastTouchFingerId = -1;
        private RectTransform leftBottomRestriction;
        private RectTransform rightTopRestriction;
        
        //触碰信息
        private int touchHeroIndex;
        private Vector3 touchHeroPoint;
        private Vector2 touchStartScreenPosition;
        private Vector3 touchStartPosition;
        private float touchStartY;
        
        
        public void Initialize()
        {
            lastTouchFingerId = -1;
            isActive = true;
            touchState = ETouchState.None;
        }

        public void Update()
        {
            if (!isActive)
            {
                return;
            }

            var touchCount = TouchWrapper.TouchCount;
            var touch0 = TouchWrapper.Touch0;
            Vector3 touch0Position = Vector3.zero;
            if (touchCount == 0)
            {
                if (touchState == ETouchState.None)
                {
                    
                }
                else if (touchState == ETouchState.Down)
                {
                    DoClick();
                }
                else if (touchState == ETouchState.Drag)
                {
                    DoDragEnd();
                }
                ClearTouchHeroData();
                touchState = ETouchState.None;
            }
            else
            {
                touch0Position = touch0.Position;
                var touchScreenPosition = new Vector2(touch0Position.x, touch0Position.y);
                if (touchState == ETouchState.None)
                {
                    /*if (leftBottomRestriction && rightTopRestriction)
                    {
                        //找到触碰边缘
                        var uiCamera = ResidentHandler.Inst.UICamera;
                        var leftBottom = leftBottomRestriction.localPosition;
                        var rightTop = rightTopRestriction.localPosition;
                        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(leftBottomRestriction.parent.GetComponent<RectTransform>(),
                                touchScreenPosition, uiCamera, out var localPos))
                        {
                            if (localPos.x > rightTop.x || localPos.y > rightTop.y || localPos.x < leftBottom.x || localPos.y < leftBottom.y)
                            {
                                return;
                            }
                        }
                    }*/
                    TryTouchHero(touchScreenPosition);
                    if (touchHeroIndex >= 0)
                    {
                        touchState = ETouchState.Down;
                        touchStartScreenPosition = touchScreenPosition;
                        touchStartPosition = touchHeroPoint;
                        touchStartY = touchHeroPoint.y;
                    }
                }
                else if (touchState == ETouchState.Down)
                {
                    var dragDistance = Vector2.Distance(touchScreenPosition, touchStartScreenPosition);
                    if (dragDistance > 15)
                    {
                        touchState = ETouchState.Drag;
                        DoDragStart();
                    }
                }
                else if (touchState == ETouchState.Drag)
                {
                    DoDrag(touch0Position);
                }
            }
        }

        private void DoClick()
        {
            if (touchHeroIndex >= 0)
            {
                DeckManager.Instance.DoClick(touchHeroIndex);
            }
        }

        private void DoDragEnd()
        {
            DeckManager.Instance.DoDragEnd();
        }

        private void DoDragStart()
        {
            if (touchHeroIndex >= 0)
            {
                DeckManager.Instance.DoDragStart(touchHeroIndex);
            }
        }

        private void DoDrag(Vector3 position)
        {
            var worldPosition = BattleHelper.GetWorldPositionByScreenPosition(CameraManager.Instance.MainCamera, new Vector2(position.x, position.y), touchStartY);
            var offset = worldPosition - touchStartPosition;
            DeckManager.Instance.DoDrag(offset);
        }
        
        private void ClearTouchHeroData()
        {
            touchHeroIndex = -1;
            touchHeroPoint = Vector3.zero;
        }
        
        private void TryTouchHero(Vector3 touchScreenPosition)
        {
            var layer = BattleConstValue.SurvivorDeckLayer;
            var ray = CameraManager.Instance.MainCamera.ScreenPointToRay(touchScreenPosition);
            var rayCastHitList = Physics.RaycastAll(ray,999,1 << layer);
            if (rayCastHitList.Length > 0)
            {
                var hit = rayCastHitList[0];
                var hero = hit.collider.GetComponent<DeckTouchTag>();
                if (hero)
                {
                    touchHeroIndex = hero.heroIndex;
                    touchHeroPoint = hit.point;
                    return;
                }
            }
        }
    }
}
